<?php


  /** represents a cicp graph node **/

    class Cicp_Graph_Node
{
    private $_menu           = null;
    private $_id             = null;
    private $_nodeProperties = array();
    private $_changed		 = false;
    public $data             = null; // var to store various data
    const SHAPE_LINE         = 'lin';
    const SHAPE_SPHERE       = 'sph';
    const SHAPE_BAR          = 'rec';
    const SHAPE_RECT         = 'rec2';

    function __construct()
    {
        // fill in default values
        $this->_menu = new Cicp_Graph_Menu(0);
        $this->_nodeProperties = array(
            'shape' 	=> 'rec',
            'loc1'		=> new Cicp_Vector3d_Coordinate(0, 0, 0),
            'loc2'		=> new Cicp_Vector3d_Coordinate(1, .5, 0),
            'color'	    => new Cicp_Vector3d_RgbColor(0, 0, 0),
            'alpha'	    => 1,
            'hl_color'	=> new Cicp_Vector3d_RgbColor(255, 255, 255),
            'index'	    => new Cicp_Vector3d_Coordinate(0, 0, 0),
            'title'	    => '-');
        $this->_changed = false;
    }

    function __toString()
    {
        $string = '';
        foreach ($this->_nodeProperties as $value) {
            if (is_array($value)) {
                foreach ($value as $subValue) {
                    $string .= $subValue . ',';
                }
            }
            else {
                $string .= $value . ',';
            }
        }
        $string .= $this->_menu->getId() . ',' . $this->getId();
        return $string;
    }

    function __clone()
    {
        // clone node properties where necessary
        foreach ($this->_nodeProperties as $index => $nP) {
            if (is_object($nP)) {
                $this->_nodeProperties[$index] = clone $nP;
            }
        }
    }

    function hasChanged()
    {
        return $this->_changed;
    }

    function equals(Cicp_Graph_Node $node) {
        foreach ($this->_nodeProperties as $index => $nP) {
            $prop = $node->getNodeProperty($index);
            if ($nP instanceof Cicp_Vector3d) {
                if ($nP->getValue(0) != $prop->getValue(0) ||
                    $nP->getValue(1) != $prop->getValue(1) ||
                    $nP->getValue(2) != $prop->getValue(2)) {
                    return false;
                }
            }
            else {
                if ($nP != $prop) {
                    return false;
                }
            }
        }
        return true;
    }

    function setId($id)
    {
        $this->_id = $id;
    }

    /**
     * assigns a graph menu to this node
     */
    function assignMenu(Cicp_Graph_Menu $menu)
    {
        $this->_menu = $menu;
    }

    function getMenu()
    {
        return $this->_menu;
    }

    function getId()
    {
        return $this->_id;
    }

    function getNodeProperty($propertyName)
    {
        if (isset($this->_nodeProperties[$propertyName])) {
            return $this->_nodeProperties[$propertyName];
        }
        else {
            return FALSE;
        }
    }

    /** node property setters **/
    private function setNodeProperty($propertyName, $value)
    {
        if (isset($this->_nodeProperties[$propertyName])) {
            // url encode strings
            if (is_string($value)) {
                $value = urlencode($value);
            }
            $this->_nodeProperties[$propertyName] = $value;
            $this->_changed = true;
        }
    }

    /**
     * sets indices for this node
     */
    function setIndex($x, $y, $z)
    {
        $this->getNodeProperty('index')->setValues($x, $y, $z);
    }

    function setShape($shapeString)
    {
        $this->setNodeProperty('shape', $shapeString);
    }

    /**
     * set node color with rgb (each value from 0-255)
     */
    function setColor($red, $green, $blue)
    {
        $this->getNodeProperty('color')->setValues($red, $green, $blue);
    }

    /**
     * set node highlight color with rgb (each value from 0-255)
     */
    function setHlColor($red, $green, $blue)
    {
        $this->getNodeProperty('hl_color')->setValues($red, $green, $blue);
    }

    /**
     * set node position in region coordinates x/y/z
     */
    function setPosition($x, $y, $z)
    {
        $this->getNodeProperty('loc1')->setValues($x, $y, $z);
    }

    function setDimensions($x, $y, $z)
    {
        $this->getNodeProperty('loc2')->setValues($x, $y, $z);
    }

    /**
     * set transparency where 0 means fully transparent and 100 no transparency
     */
    function setAlpha($alpha)
    {
        $this->setNodeProperty('alpha', $alpha);
    }

    /**
     * set node title
     */
    function setTitle($title)
    {
        $this->setNodeProperty('title', $title);
    }
}
